#include "favorite.h"
#include "ui_favorite.h"

Favorite::Favorite(Dialog_Adress_Manager * Adress_Manager, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Favorite)
{
    ui->setupUi(this);

    connect(ui->pushButton_Add_Favorite, SIGNAL(clicked()), this, SLOT(Add_Favorite()));
    connect(ui->pushButton_Remove_Favorite, SIGNAL(clicked()), this, SLOT(Remove_Favorite()));

    this->Adress_Manager = Adress_Manager;
}
///////////////////////////////////////////////////////////////////////////////////
Favorite::~Favorite()
{
    delete ui;
}
///////////////////////////////////////////////////////////////////////////////////
void Favorite::Change_Theme(QString theme_name, QString style)
{
    ui->pushButton_Add_Favorite->setIcon(QIcon(theme_name+"/ICONS/plus.png"));
    ui->pushButton_Remove_Favorite->setIcon(QIcon(theme_name+"/ICONS/less.png"));

    style += "#pushButton_Remove_Favorite, #pushButton_Add_Favorite, #pushButton_Save_Favorite, #pushButton_Modify_Favorite { border: none; background: url(\""+theme_name+"/BUTTONS/50x50_empty_normal2.png\") center no-repeat }";
    style += "#pushButton_Remove_Favorite:hover, #pushButton_Add_Favorite:hover, #pushButton_Save_Favorite:hover, #pushButton_Modify_Favorite:hover { border: none; background: url(\""+theme_name+"/BUTTONS/50x50_empty_hover2.png\") center no-repeat }";
    style += "#pushButton_Remove_Favorite:!enabled, #pushButton_Add_Favorite:!enabled, #pushButton_Save_Favorite:!enabled, #pushButton_Modify_Favorite:!enabled { border: none; background: url(\""+theme_name+"/BUTTONS/50x50_empty_disabled2.png\") center no-repeat }";

    setStyleSheet(style);
}
///////////////////////////////////////////////////////////////////////////////////
void Favorite::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
///////////////////////////////////////////////////////////////////////////////////
void Favorite::Add_Favorite()
{
    Adress * new_favorite = new Adress();
    Adress_Manager->setAdress(new_favorite);
    int result = Adress_Manager->exec();
    if ( result != 0 )
    {
        int row = ui->tableWidget_favorites->rowCount();
        ui->tableWidget_favorites->insertRow(row);
        QTableWidgetItem * item_addr = new QTableWidgetItem();
        item_addr->setText(QString::fromStdString(new_favorite->ToString()));

        QTableWidgetItem * item_name = new QTableWidgetItem();
        item_name->setText(QString::fromStdString(new_favorite->Get_Name()));
       // item_name->setData(Qt::UserRole, QVariant(new_favorite));

        ui->tableWidget_favorites->setItem(row, 1, item_addr);
        ui->tableWidget_favorites->setItem(row, 0, item_name);
        ui->tableWidget_favorites->selectRow(row);
        ui->tableWidget_favorites->show();
        ui->pushButton_Remove_Favorite->show();
    }
    else
    {
        delete new_favorite;
    }
}
///////////////////////////////////////////////////////////////////////////////////
void Favorite::Remove_Favorite()
{
    int row = ui->tableWidget_favorites->currentRow();
    if (row != -1)
    {
        QTableWidgetItem * item = ui->tableWidget_favorites->item(row,0);
        //Adress *a = (Adress*)item->data(Qt::UserRole).value();

        //remove favorite from database

        ui->tableWidget_favorites->removeRow(row);
    }
}
///////////////////////////////////////////////////////////////////////////////////
